package com.app.babygo;

import com.app.babygo.help.CameraHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class CameraBasePage extends Activity {
	
	protected SurfaceView AchievementImage;
	protected Button OkBtn;
	protected Button CancelBtn;
	protected Context ViewContext;
	protected CameraHelper camera;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_camera_page_layout);
		createUI();
	}

	protected void createUI() {
		ViewContext = this.getApplicationContext();
		AchievementImage = (SurfaceView) this.findViewById(R.id.picture_surface_view);
		OkBtn = (Button) this.findViewById(R.id.confirm_btn);
		CancelBtn = (Button) this.findViewById(R.id.cancel_btn);
		camera = new CameraHelper(AchievementImage);
		previewStage();
	}

	protected void previewStage() {
		OkBtn.setText("OK");
		OkBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				previewStageOK(v);
			}		
		});
		
		CancelBtn.setText("Cancel");
		CancelBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				previewStageCancel(v);
			}			
		});
	}
	
	protected void confirmStage() {
		OkBtn.setText("Done");
		OkBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				confirmStageOK(v);				
			}		
		});
		
		CancelBtn.setText("Re-take");
		CancelBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				confirmStageCancel(v);
			}
		});
	}
	
	protected void previewStageOK(View v){
		//stop preview. enter picture confirm stage.
		camera.stopPreview();
		confirmStage();
	}
	
	protected void previewStageCancel(View v){
		exit();	
	}
	
	protected void confirmStageOK(View v){
		exit();
	}
	
	protected void confirmStageCancel(View v){
		//restart. enter picture preview stage.
		camera.startPreview();
		previewStage();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.child_info, menu);
		return true;
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		exit();
	}
	
	protected void exit(){
		camera.close();
		finish();
	}

}
